<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

/**             
 * 行为管理控制器
 * @package app\admin\controller
 */
class Department extends Admin
{

    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('member_department')->where($map)->order('department_id desc')->paginate();

        // 分页数据
        $page = $data_list->render();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('公司性质')// 设置页面标题
            ->setTablename('member_department')// 设置数据表名
            ->setSearch(['department' => '所在部门'])// 设置搜索参数
            ->setPrimaryKey('department_id')
            ->addColumns([ // 批量添加列
                ['department_id', '部门编号'],
                ['department', '所在部门'],
                ['status', '当前状态','switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete')// 批量添加顶部按钮
            -> addTopButton('enable',['title'=>'启用','icon' => 'fa fa-fw fa-unlock'])
            -> addTopButton('disable',['title'=>'禁用','icon'=>'fa fa-fw fa-unlock-alt'])
            ->addRightButtons('edit')// 批量添加右侧按钮
            ->addRightButton('delete',['icon' => 'fa fa-times', 'title' => '删除', 'href' => url('delete_department', ['department_id' => '__id__'])])
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }

    //新增部门
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Department');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            $data['createtime']=time();
            $res=Db::name('member_department')->insert($data);
            if ($res) {
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->setPageTitle('新增部门')// 设置页面标题
                ->addFormItems([ // 批量添加表单项
                    ['text', 'department', '所在部门', '必填'],
                ])
                ->layout(['department'=>4])
                ->fetch();
    }
    //编辑
    public function edit($id = null){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Department');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            $res=Db::name('member_department')->update($data);
            if ($res) {
                return $this->success('更新成功', cookie('__forward__'));
            } else {
                return $this->error('更新失败');
            }
        }
        $info = Db::name('member_department')->where('department_id',$id)->find();
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑部门')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden','department_id'],
                ['text', 'department', '所在部门', '必填'],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
            ])
            ->setFormData($info) // 设置表单数据
            ->layout(['department'=>4])
            ->fetch();
    }
    //单个删除
    public function delete_department(){
        $id = $this->request->param('department_id');
        $res=Db::name("member_department")->where('department_id',$id)->delete();
        if ($res) {
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
    //批量删除
    public function delete($record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("member_department")->where('department_id',$values)->delete();
        }
        if ($res) {
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
    //批量启用
    public function enable($record = [])
    {
        $data=array('status'=>1);
        $this->auditing($data);

    }
    //批量禁用
    public function disable($record = [])
    {
        $data['status']=0;
        $this->auditing($data);

    }
    public function auditing($data){
        $ids= $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("member_department")->where('department_id',$values)->update($data);
        }
        if ($res) {
            return $this->success('操作成功',cookie('__forward__'));
        } else {
            return $this->error('操作失败');
        }
    }
}